/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qhq.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qhq.entity.SchoolDept;
import com.thinkgem.jeesite.modules.qhq.dao.SchoolDeptDao;
import com.thinkgem.jeesite.modules.qhq.entity.DeptParentlist;
import com.thinkgem.jeesite.modules.qhq.dao.DeptParentlistDao;

/**
 * 学部管理Service
 * @author 秦华权
 * @version 2017-11-27
 */
@Service
@Transactional(readOnly = true)
public class SchoolDeptService extends CrudService<SchoolDeptDao, SchoolDept> {

	@Autowired
	private DeptParentlistDao deptParentlistDao;
	
	public SchoolDept get(String id) {
		SchoolDept schoolDept = super.get(id);
		schoolDept.setDeptParentlistList(deptParentlistDao.findList(new DeptParentlist(schoolDept)));
		return schoolDept;
	}
	
	public List<SchoolDept> findList(SchoolDept schoolDept) {
		return super.findList(schoolDept);
	}
	
	public Page<SchoolDept> findPage(Page<SchoolDept> page, SchoolDept schoolDept) {
		return super.findPage(page, schoolDept);
	}
	
	@Transactional(readOnly = false)
	public void save(SchoolDept schoolDept) {
		super.save(schoolDept);
		for (DeptParentlist deptParentlist : schoolDept.getDeptParentlistList()){
			if (deptParentlist.getId() == null){
				continue;
			}
			if (DeptParentlist.DEL_FLAG_NORMAL.equals(deptParentlist.getDelFlag())){
				if (StringUtils.isBlank(deptParentlist.getId())){
					deptParentlist.setSchoolDept(schoolDept);
					deptParentlist.preInsert();
					deptParentlistDao.insert(deptParentlist);
				}else{
					deptParentlist.preUpdate();
					deptParentlistDao.update(deptParentlist);
				}
			}else{
				deptParentlistDao.delete(deptParentlist);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(SchoolDept schoolDept) {
		super.delete(schoolDept);
		deptParentlistDao.delete(new DeptParentlist(schoolDept));
	}
	
}